/*
 * Copyright 2012-2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot.autoconfigure.security.reactive;

import java.util.EnumSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import reactor.core.publisher.Mono;

import org.springframework.boot.autoconfigure.security.StaticResourceLocation;
import org.springframework.security.web.server.util.matcher.OrServerWebExchangeMatcher;
import org.springframework.security.web.server.util.matcher.PathPatternParserServerWebExchangeMatcher;
import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatcher;
import org.springframework.util.Assert;
import org.springframework.web.server.ServerWebExchange;

/**
 * 用于创建匹配常用位置中静态资源的 {@link ServerWebExchangeMatcher}。
 * 由 {@link PathRequest#toStaticResources()} 返回。
 *
 * @author Madhura Bhave
 * @since 2.0.0
 * @see PathRequest
 */
public final class StaticResourceRequest {

	static final StaticResourceRequest INSTANCE = new StaticResourceRequest();

	private StaticResourceRequest() {
	}

	/**
	 * 返回一个匹配器，包含所有常用的 {@link StaticResourceLocation} 位置。
	 * 可以使用 {@link StaticResourceServerWebExchange#excluding(StaticResourceLocation, StaticResourceLocation...)}
	 * 方法排除特定位置。例如：<pre class="code">
	 * PathRequest.toStaticResources().atCommonLocations().excluding(StaticResourceLocation.CSS)
	 * </pre>
	 * @return 配置好的 {@link ServerWebExchangeMatcher}
	 */
	public StaticResourceServerWebExchange atCommonLocations() {
		return at(EnumSet.allOf(StaticResourceLocation.class));
	}

	/**
	 * 返回一个匹配器，包含指定的 {@link StaticResourceLocation} 位置。例如：<pre class="code">
	 * PathRequest.toStaticResources().at(StaticResourceLocation.CSS, StaticResourceLocation.JAVA_SCRIPT)
	 * </pre>
	 * @param first 第一个要包含的位置
	 * @param rest 其他要包含的位置
	 * @return 配置好的 {@link ServerWebExchangeMatcher}
	 */
	public StaticResourceServerWebExchange at(StaticResourceLocation first, StaticResourceLocation... rest) {
		return at(EnumSet.of(first, rest));
	}

	/**
	 * 返回一个匹配器，包含指定的 {@link StaticResourceLocation} 位置集合。例如：<pre class="code">
	 * PathRequest.toStaticResources().at(locations)
	 * </pre>
	 * @param locations 要包含的位置集合
	 * @return 配置好的 {@link ServerWebExchangeMatcher}
	 */
	public StaticResourceServerWebExchange at(Set<StaticResourceLocation> locations) {
		Assert.notNull(locations, "Locations must not be null");
		return new StaticResourceServerWebExchange(new LinkedHashSet<>(locations));
	}

	/**
	 * 用于匹配资源 {@link StaticResourceLocation} 位置的服务器 Web 交换匹配器。
	 */
	public static final class StaticResourceServerWebExchange implements ServerWebExchangeMatcher {

		private final Set<StaticResourceLocation> locations;

		private StaticResourceServerWebExchange(Set<StaticResourceLocation> locations) {
			this.locations = locations;
		}

		/**
		 * 返回一个基于当前实例但排除指定位置的新 {@link StaticResourceServerWebExchange}。
		 * @param first 第一个需要排除的位置
		 * @param rest 其他需要排除的位置
		 * @return 新的 {@link StaticResourceServerWebExchange} 实例
		 */
		public StaticResourceServerWebExchange excluding(StaticResourceLocation first, StaticResourceLocation... rest) {
			return excluding(EnumSet.of(first, rest));
		}

		/**
		 * 返回一个基于当前实例但排除指定位置集合的新 {@link StaticResourceServerWebExchange}。
		 * @param locations 需要排除的位置集合
		 * @return 新的 {@link StaticResourceServerWebExchange} 实例
		 */
		public StaticResourceServerWebExchange excluding(Set<StaticResourceLocation> locations) {
			Assert.notNull(locations, "Locations must not be null");
			Set<StaticResourceLocation> subset = new LinkedHashSet<>(this.locations);
			subset.removeAll(locations);
			return new StaticResourceServerWebExchange(subset);
		}

		private List<ServerWebExchangeMatcher> getDelegateMatchers() {
			return getPatterns().map(PathPatternParserServerWebExchangeMatcher::new).collect(Collectors.toList());
		}

		private Stream<String> getPatterns() {
			return this.locations.stream().flatMap(StaticResourceLocation::getPatterns);
		}

		@Override
		public Mono<MatchResult> matches(ServerWebExchange exchange) {
			OrServerWebExchangeMatcher matcher = new OrServerWebExchangeMatcher(getDelegateMatchers());
			return matcher.matches(exchange);
		}

	}

}
